package com.example.servlet;

import com.example.enums.PetBreedEnum;
import com.example.enums.PetSexEnum;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@WebServlet(value = "/addPetPageServlet")
public class AddPetPageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取品种列表
        PetBreedEnum[] petBreeds = PetBreedEnum.values();
        List<HashMap<String,Object>> breedList = new ArrayList<>();
        for (PetBreedEnum breed : petBreeds) {
            HashMap<String,Object> breedMap = new HashMap<>();
            breedMap.put("value",breed.getValue());
            breedMap.put("name",breed.name());
            breedList.add(breedMap);
        }
        //获取性别
        List<HashMap<String,Object>> sexList = new ArrayList<>();
        PetSexEnum[] petBreedEnums = PetSexEnum.values();
        for (PetSexEnum sexEnum : petBreedEnums) {
            HashMap<String,Object> map = new HashMap<>();
            map.put("value",sexEnum.getValue());
            map.put("name",sexEnum.name());
            sexList.add(map);
        }
        req.setAttribute("breedList",breedList);
        req.setAttribute("sexList",sexList);
        req.getRequestDispatcher("/addPet.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
